package com.fingard.dsp.bank.reqrespobj.single;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.specialDeal.BankAreaHelper;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.RequestBase;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.single.detail.ReqQrySglBatPayDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;

public class ReqQrySingle extends RequestBase{

	public String askNumber;
	public String bgnDate;
	public String endDate;
	public String payArea;
	public String payAct;
	public String payName;
	public String payCur;

	public String recArea;
	public String recAct;
	public String recCur;

	public String ownBankCode;
	public String payBankCode;
	public String recBankCode;

	public String payAmount;
	public String payState;

	public String sameBnk;//0为跨行 1为同行

	public String certType;
	public String certNum;
	public String authCode;
	public String insuPolicyNum;
	public String memo;
	/**手机号码*/
    public String cellPhone;

    public String isPrivate;

    /**查询方式，ByDate标识按日查询*/
    public String queryType;

    /**是否按日查询*/
    public boolean isByDate;

	/**保险公司指定商户订单号*/
	public String officePurpose;

	/**平台号*/
	public String circMchNo;

	public String orderNo;

	public ReqQrySingle(ReqQrySglBatPayDetail p_detail, ReqRespHeader p_header){
    	reqHeader = p_header;
    	askNumber = p_detail.reqSeqID;
        orderNo = p_detail.orderNo;
    	bgnDate = p_detail.bgnDate;
    	payArea = p_detail.payArea;

		payAct = p_detail.payAct;
		payName = p_detail.payName;
		payCur = p_detail.payCur;

		officePurpose = p_detail.officePurpose;
		circMchNo = p_detail.circMchNo;
		payAmount = p_detail.payAmount;

		payBankCode = p_detail.payBankCode;
		recBankCode = p_detail.recBankCode;
		certType = p_detail.certType;
		certNum = p_detail.certNum;
		authCode = p_detail.authCode;
		insuPolicyNum = p_detail.insuPolicyNum;
		memo = p_detail.memo;

		if(reqHeader.transType.equals("1908") || reqHeader.transType.equals("6908") || reqHeader.transType.equals("1988") || reqHeader.transType.equals("1944") || reqHeader.transType.equals("1998")){
			ownBankCode = payBankCode;
    	}
    	else if(reqHeader.transType.equals("9108") || reqHeader.transType.equals("9188") || reqHeader.transType.equals("9144") || reqHeader.transType.equals("9145") || reqHeader.transType.equals("9146") || reqHeader.transType.equals("9198")){
    		ownBankCode = recBankCode;
    	}

		payState = p_detail.payState;
    }

	private XmlTextReader xmlReader;
	public ReqQrySingle(XmlTextReader p_xmlReader, ReqRespHeader p_header) throws Exception{
		reqHeader = p_header;
		xmlReader = p_xmlReader;
		isByDate = false;

		if(p_xmlReader.read(PATH_Detail)){//"/ATSYH/TransReq/TransParam/DetailRecord"
			askNumber = p_xmlReader.readedXElement.getSingleText("ReqSeqID");
			orderNo = p_xmlReader.readedXElement.getSingleText("OrderNumber");
			bgnDate = p_xmlReader.readedXElement.getSingleText("BgnDate");
			payArea = p_xmlReader.readedXElement.getSingleText("PayArea");
            if (FGBiz.dspCenter.convertBankArea) {
                BankAreaHelper bankAreaHelper=new BankAreaHelper(p_header.bankCode,payArea,"");
                bankAreaHelper.toBank();
                payArea=bankAreaHelper.bankAreaCode;
            }
			payAct = p_xmlReader.readedXElement.getSingleText("PayAct");
            payName = p_xmlReader.readedXElement.getSingleText("PayName");
			payCur = p_xmlReader.readedXElement.getSingleText("PayCur");

			recArea = p_xmlReader.readedXElement.getSingleText("RecArea");
            if (FGBiz.dspCenter.convertBankArea) {
                BankAreaHelper bankAreaHelper=new BankAreaHelper(p_header.bankCode,recArea,"");
                bankAreaHelper.toBank();
                recArea=bankAreaHelper.bankAreaCode;
            }
			recAct = p_xmlReader.readedXElement.getSingleText("RecAct");
			recCur = p_xmlReader.readedXElement.getSingleText("RecCur");
			payAmount = p_xmlReader.readedXElement.getSingleText("PayAmount");

			payBankCode = p_xmlReader.readedXElement.getSingleTextNotNull("PayBankCode");
			recBankCode = p_xmlReader.readedXElement.getSingleTextNotNull("RecBankCode");

			if(reqHeader.transType.equals("1908") || reqHeader.transType.equals("6908") || reqHeader.transType.equals("1988") || reqHeader.transType.equals("1944") || reqHeader.transType.equals("1998")){
				ownBankCode = payBankCode;
	    	}
	    	else if(reqHeader.transType.equals("9108") || reqHeader.transType.equals("9188") || reqHeader.transType.equals("9144") || reqHeader.transType.equals("9145") || reqHeader.transType.equals("9146") || reqHeader.transType.equals("9198")){
	    		ownBankCode = recBankCode;
	    	}

			payState = p_xmlReader.readedXElement.getSingleText("PayState");
			if(StringHelper.isNullOrEmpty(payState)){
				payState = "5";
			}

			cellPhone = p_xmlReader.readedXElement.getSingleTextNotNull("CellPhone");
	    	if(cellPhone.length() == 0){
	    		cellPhone = p_xmlReader.readedXElement.getSingleTextNotNull("Mobile");
	    	}
			sameBnk = p_xmlReader.readedXElement.getSingleTextNotNull("SameBnk");
	    	isPrivate=p_xmlReader.readedXElement.getSingleText("IsPrivate");

	    	queryType=p_xmlReader.readedXElement.getSingleTextNotNull("QueryType");
	    	if("ByDate".equalsIgnoreCase(queryType)){
	    		isByDate = true;
	    	}
	    	endDate = p_xmlReader.readedXElement.getSingleTextNotNull("EndDate");
			officePurpose =p_xmlReader.readedXElement.getSingleTextNotNull("OfficePurpose");
			circMchNo =p_xmlReader.readedXElement.getSingleTextNotNull("circMchNo");
			certType = p_xmlReader.readedXElement.getSingleTextNotNull("CertType");
			certNum = p_xmlReader.readedXElement.getSingleTextNotNull("CertNum");
			authCode = p_xmlReader.readedXElement.getSingleTextNotNull("AuthCode");
			insuPolicyNum = p_xmlReader.readedXElement.getSingleTextNotNull("InsuPolicyNum");
			memo = p_xmlReader.readedXElement.getSingleTextNotNull("Memo");

		}
		else{
			throw new Exception("不存在节点："+PATH_Detail);
		}
	}
	
	public ReqQrySingle(HashMap<String, String> p_hashParam, ReqRespHeader p_header) throws Exception{
		reqHeader = p_header;
		//xmlReader = p_xmlReader;
		if(p_hashParam.containsKey("RdSeq")){
			askNumber = p_hashParam.get("RdSeq");
		}
		if(p_hashParam.containsKey("OurBankAccount")){
		    String transType = p_hashParam.get("TransType");
		    if ("QueryTransferPay".equalsIgnoreCase(transType)) {
                payAct = p_hashParam.get("OurBankAccount");
            } else {
                recAct = p_hashParam.get("OurBankAccount");
            }
		}
		if(p_hashParam.containsKey("PayState")){
			payState = p_hashParam.get("PayState");
		}
		if(p_hashParam.containsKey("BgnDate")){
			bgnDate = p_hashParam.get("BgnDate");
		}
		else if(p_hashParam.containsKey("PaySentDate")){
			bgnDate = p_hashParam.get("PaySentDate");
		}
		if(StringHelper.isNullOrEmpty(payState)){
			payState = "5";
		}
		if(p_hashParam.containsKey("Amount")){
			payAmount = p_hashParam.get("Amount");
		}
		else if(p_hashParam.containsKey("PayAmount")){
			payAmount = p_hashParam.get("PayAmount");
		}
		if(p_hashParam.containsKey("CellPhone")){
			cellPhone = p_hashParam.get("CellPhone");
		}
		else if(p_hashParam.containsKey("Mobile")){
			cellPhone = p_hashParam.get("Mobile");
		}
		if(p_hashParam.containsKey("QueryType")){
			queryType = p_hashParam.get("QueryType");
	    	if("ByDate".equalsIgnoreCase(queryType)){
	    		isByDate = true;
	    	}
	    	if(p_hashParam.containsKey("EndDate")){
	    		endDate = p_hashParam.get("EndDate");
			}
		}
		if(p_hashParam.containsKey("OfficePurpose")){
			officePurpose = p_hashParam.get("OfficePurpose");
		}
		if(p_hashParam.containsKey("CircMchNo")){
			circMchNo = p_hashParam.get("CircMchNo");
		}
		if(p_hashParam.containsKey("CertType")){
			certType = p_hashParam.get("CertType");
		}
		if(p_hashParam.containsKey("CertNum")){
			certNum = p_hashParam.get("CertNum");
		}
		if(p_hashParam.containsKey("AuthCode")){
			authCode = p_hashParam.get("AuthCode");
		}
		if(p_hashParam.containsKey("InsuPolicyNum")){
			insuPolicyNum = p_hashParam.get("InsuPolicyNum");
		}
		if(p_hashParam.containsKey("Memo")){
			memo = p_hashParam.get("Memo");
		}
        if(p_hashParam.containsKey("PayAct")){
            payAct = p_hashParam.get("PayAct");
        }
        if(p_hashParam.containsKey("PayName")){
            payName = p_hashParam.get("PayName");
        }
	}
	
	public Date getBgnDateTime() throws ParseException{
		if(isByDate){
			return Format.Date10Format.parse(bgnDate);
		}else{
			return Format.DateTimeFormat.parse(bgnDate);
		}
	}
	
	public Date getEndDateTime() throws ParseException{
		if(isByDate){
			return Format.Date10Format.parse(endDate);
		}else{
			return Format.DateTimeFormat.parse(endDate);
		}
	}
	
	public String getOriReqXml(){
		return xmlReader.getOriXmlString();
	}
	
	public ReqQrySingle(ReqQryBatPayRec p_qryBat){
    	reqHeader = p_qryBat.reqHeader;
    	askNumber = p_qryBat.reqBatchNO;
    	bgnDate = p_qryBat.bgnDate;
    	if("1998".equals(p_qryBat.reqHeader.transType) || "1944".equals(p_qryBat.reqHeader.transType) || "6944".equals(p_qryBat.reqHeader.transType)){
    		payArea = p_qryBat.ownArea;
    		payAct = p_qryBat.ownAct;
    		payBankCode = p_qryBat.ownBankCode;
    	}else if("9198".equals(p_qryBat.reqHeader.transType) || "9144".equals(p_qryBat.reqHeader.transType) || "9145".equals(p_qryBat.reqHeader.transType) || "9146".equals(p_qryBat.reqHeader.transType)){
    		recArea = p_qryBat.ownArea;
    		recAct = p_qryBat.ownAct;
    		recBankCode = p_qryBat.ownBankCode;
    	}
    	ownBankCode = p_qryBat.ownBankCode;
		payAmount = p_qryBat.totalAmt;
		payState = p_qryBat.payState;
		officePurpose = p_qryBat.officePurpose;
		circMchNo = p_qryBat.circMchNo;
		certType = p_qryBat.certType;
		certNum = p_qryBat.certNum;
		authCode = p_qryBat.authCode;
		insuPolicyNum = p_qryBat.insuPolicyNum;
		memo = p_qryBat.memo;
    }
}
